package com.atguigu.gmall.mq.controller;


import com.atguigu.gmall.common.config.entity.GmallCorrelationData;
import com.atguigu.gmall.common.config.service.RabbitService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.mq.config.DeadLetterMqConfig;
import com.atguigu.gmall.mq.config.DelayMqConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/mq")
public class MQController {

    @Autowired
    private RabbitService rabbitService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //
    @GetMapping("/sendConfirm")
    public Result sendConfirm(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());


        rabbitService.sendMessage
                ("exchange.confirm","routing111.confirm",format);

//        @RabbitListener(bindings = @QueueBinding(
//                value = @Queue(value = "queue.confirm",durable = "true",autoDelete = "false"),
//                exchange = @Exchange(value = "exchange.confirm",durable = "true",autoDelete = "false"),
//                key = "routing.confirm"
//        ))

        return Result.ok();
    }

    //发送死信
    @GetMapping("sendDeadLetter")
    public Result sendDeadLetter(){


//        rabbitTemplate.convertAndSend(DeadLetterMqConfig.exchange_dead,DeadLetterMqConfig.routing_dead_1
//        ,"1112",m -> {
//            m.getMessageProperties().setExpiration("5000");
//            return m;
//                });
        rabbitTemplate.convertAndSend(DeadLetterMqConfig.exchange_dead,DeadLetterMqConfig.routing_dead_1
                ,"1112qqq");

        return Result.ok();
    }

    @GetMapping("sendDelayed")
    public Result sendDelayed(){

        GmallCorrelationData gmallCorrelationData = new GmallCorrelationData();
        String correlationId = UUID.randomUUID().toString();
        gmallCorrelationData.setId(correlationId);

        rabbitTemplate.convertAndSend(DelayMqConfig.exchange_delay,DelayMqConfig.routing_delay,"12345"
        ,m -> {
            m.getMessageProperties().setDelay(50000);
            return m;
                },gmallCorrelationData);

        return Result.ok();
    }
}
